using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;
//using UnityEditor;
using System.IO;
public class U3DWindow
{
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);
    [DllImport("user32.dll")]
    public static extern System.IntPtr FindWindow(string IpClassName, string IpWindowName);
    [DllImport("user32.dll")]
    static extern IntPtr GetActiveWindow();
    // not used rigth now
    //const uint SWP_NOMOVE = 0x2;
    //const uint SWP_NOSIZE = 1;
    //const uint SWP_NOZORDER = 0x4;
    //const uint SWP_HIDEWINDOW = 0x0080;

    const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
    const int SW_SHOWMAXIMIZED = 3;//最大化
    const int SW_SHOWRESTORE = 1;//还原
    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;
    const int WS_POPUP = 0x800000;
    const string programName = "Test";
    public static void SetWindow(int x, int y, int width, int height)
    {
        IntPtr hWnd = FindWindow(null, programName);
        if (hWnd != null)
        {
            //SetWindowLong(hWnd,GWL_STYLE,WS_BORDER);
            SetWindowPos(hWnd, -1, x, y, width, height, SWP_SHOWWINDOW);
        }
    }
    public static void SetWindow(Rect rect)
    {
        SetWindow((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
    }
    public static void SetWindowTop(int Topmost,int left, int top, int width, int height,bool isFullScreen)
    {
      
        IntPtr hWnd = FindWindow(null, programName);
        if (hWnd != null)
        {
            Screen.fullScreen = isFullScreen;
            SetWindowLong(hWnd, GWL_STYLE, WS_BORDER);//importants:sets a new window stytle!
            SetWindowPos(hWnd, Topmost, left, top, width, height, SWP_SHOWWINDOW);
        }
    }
    public static void SetWindowTop2(int x, int y, int width, int height)
    {
        //设置窗口不可拖动
        //SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
        SetWindowPos(GetForegroundWindow(), -1, x, y, width, height, SWP_SHOWWINDOW);
    }

    public static void OnClickMinimize()
    {

        ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
    }

    public static void OnClickMaximize()
    {

        ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
    }

    public static void OnClickRestore()
    {

        ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE);
    }



    //---------------------------------------------------------------------------------------------------------------------------




    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetParent(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

    [DllImport("kernel32.dll")]
    public static extern void SetLastError(uint dwErrCode);


    public static IntPtr GetProcessWnd()
    {
        IntPtr ptrWnd = IntPtr.Zero;
        uint pid = (uint)Process.GetCurrentProcess().Id;  // 当前进程 ID

        bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam)
        {
            uint id = 0;

            if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref id);
                if (id == lParam)    // 找到进程对应的主窗口句柄
                {
                    ptrWnd = hwnd;   // 把句柄缓存起来
                    SetLastError(0);    // 设置无错误
                    return false;   // 返回 false 以终止枚举窗口
                }
            }

            return true;

        }), pid);

        return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;
    }



    public static void SetWindowTopNew(int Topmost, int left, int top, int width, int height, bool isFullScreen)
    {
        //IntPtr hWnd = GetProcessWnd();
        IntPtr hWnd = FindWindow(null, programName);
        if (hWnd != null)
        {
            Screen.fullScreen = isFullScreen;
            SetWindowLong(GetProcessWnd(), GWL_STYLE, WS_BORDER);//importants:sets a new window stytle!
            SetWindowPos(GetProcessWnd(), Topmost, left, top, width, height, SWP_SHOWWINDOW);
        }
    }


}